import Comment from "./comment";
export default class Block {
  // 控制格子方块
  constructor(
    x,
    y,
    width = Comment.blockSize,
    height = Comment.blockSize,
    imgSrc = require("@/assets/block.png")
  ) {
    this.x = x;
    this.y = y;
    this.width = width;
    this.height = height;
    this.imgSrc = imgSrc;
    // 把方块画到画布
    this.image = new Image();
    this.image.src = this.imgSrc;
    // 定义一个状态是否加载完成
    this.isReady = false;
    // 等地图加载完
    this.image.onload = () => {
      // 告诉画布可以画了
      this.isReady = true;
    };
  }

  // 给方块一个出现在页面的方法
  render(context) {
    // context是画笔 画东西
    // context.drawImage("对象", "X坐标", "Y坐标", "宽度", "高度");
    if (this.isReady) {
      context.drawImage(this.image, this.x, this.y, this.width, this.height);
    }
  }
}
